package logic

import (
	"context"
	"fmt"
	"month2/C/rpc_C/order"
	"net/http"

	"month2/C/api_C/internal/svc"
	"month2/C/api_C/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AliapyBackLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAliapyBackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AliapyBackLogic {
	return &AliapyBackLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AliapyBackLogic) AliapyBack(w http.ResponseWriter, r *http.Request) (resp *types.AliapyBackResponse, err error) {
	fmt.Println("支付宝回调")
	fmt.Println(r)
	OrderSn := r.FormValue("out_trade_no")
	fmt.Println(OrderSn)
	Status := r.FormValue("trade_status")
	fmt.Println(Status)
	if Status == "TRADE_SUCCESS" {
		Status = "已支付"
	}
	_, err = l.svcCtx.CRpc.UpdateOrderStatus(l.ctx, &order.UpdateOrderStatusRequest{
		OrderSn: OrderSn,
		Status:  Status,
	})
	return
}
